package spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 创建时间：2025-05-09 13:37
 */
@Service
public class RewardStrategyContext {
    private final Map<String, RewardStrategy> strategyMap;
    private final RewardStrategy defaultStrategy;

    @Autowired
    public RewardStrategyContext(List<RewardStrategy> strategies) {
        // 注入默认策略
        this.defaultStrategy = strategies.stream()
                .filter(s -> Type.DEFAULT.name().equals(s.getStrategyType()))
                .findFirst()
                .orElseThrow(() -> new IllegalStateException("未找到默认策略"));
        // 注入具体策略至map
        this.strategyMap = strategies.stream()
                .filter(s -> !Type.DEFAULT.name().equals(s.getStrategyType()))
                .collect(Collectors.toMap(RewardStrategy::getStrategyType, s -> s));
    }

    public RewardStrategy getStrategy(String strategyType) {
        return strategyMap.getOrDefault(strategyType, defaultStrategy);
    }
}
